﻿using MediatR;
using Newtonsoft.Json;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using Warehouse.Read.API.Applcation.Command.MovelibraryCommand;
using static WMSpublic.ParseAPI;
using WMSpublic;

namespace Warehouse.Read.API.Applcation.CommandHandlers.MovelibraryCommandHandlers
{
    /// <summary>
    /// 反填移库信息
    /// </summary>
    public class FanMovelibraryCommandHandlers:IRequestHandler<FanMovelibraryCommand,List<MovelibraryFanDTO>>
    {
        private readonly IMovelibraryRepository _movelibraryRep;
        private readonly IMoveProductRepository _moveProductRep;

        public FanMovelibraryCommandHandlers(IMovelibraryRepository movelibraryRep, IMoveProductRepository moveProductRep)
        {
            _movelibraryRep = movelibraryRep;
            _moveProductRep = moveProductRep;
        }

        public async Task<List<MovelibraryFanDTO>> Handle(FanMovelibraryCommand request, CancellationToken cancellationToken)
        {
            //var res = await _inventRep.GetModelAsync(request.id);
            //1.先反填盘点 2.根据盘点id找到盘点产品的产品
            var pr = await _moveProductRep.GetAllAsyns();
            List<Products> products = await GetProductsList(1, 1000);
            var res = from a in await _movelibraryRep.GetAllAsyns()
                      where a.MovelibraryId == request.MovelibraryId
                      select new MovelibraryFanDTO
                      {
                          MovelibraryId=a.MovelibraryId,
                          MovelibraryCode = a.MovelibraryCode,
                          MovelibraryType = a.MovelibraryType,
                          MorderNumber = a.MorderNumber,
                          MakePerson = a.MakePerson,
                          MarkTime = a.MarkTime,
                          ORremark = a.ORremark,
                          ReasonForReview = a.ReasonForReview,
                          DocumentStatus = a.DocumentStatus,
                          IsRemoved = a.IsRemoved,
                          office_id = a.office_id,
                          AddName = a.AddName,
                          AddTime = a.AddTime,
                          listProduct = (from b in pr
                             join hh in products on b.ProductsID equals hh.ProductsID
                             where b.MovelibraryCode == a.MovelibraryCode
                             select new MoveProductDTO
                             {
                                 MoveProductId=b.MoveProductId,
                                 MovelibraryCode=b.MovelibraryCode,
                                 ProductsID=b.ProductsID,
                                 MoveProductNum = b.MoveProductNum,
                                 Movelocation = b.Movelocation,
                                 office_id = b.office_id,
                                 user_id = b.user_id,
                                 ProductsCode=hh.ProductsCode,
                                 ProductsName = hh.ProductsName,
                                 VendorsCode = hh.VendorsCode,
                                 InteriorCode = hh.InteriorCode,
                                 Specs = hh.Specs,
                                 ProductTypeID = hh.ProductTypeID,
                                 StorageUnits = hh.StorageUnits,
                                 Upperlimit = hh.Upperlimit,
                                 Lowerlimit = hh.Lowerlimit,
                                 WrapType = hh.WrapType,
                                 Price = hh.Price,
                                 Weight = hh.Weight,
                                 WarehouseId = hh.WarehouseId,
                                 StorehouseId = hh.StorehouseId,
                                 VendorId = hh.VendorId,
                                 ClientId = hh.ClientId,
                                 Describe = hh.Describe,
                                 ProcurementNum = hh.ProcurementNum,
                             }).ToList()
                      };
            return res.ToList();
        }
        /// <summary>
        /// 产品
        /// </summary>
        /// <returns></returns>
        public async Task<dynamic> GetProductsList(int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNiIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMiIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLku5jmtJvlmIkiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.JtV8g_mbUcJgyPC793AMsvjLkmkeuiRR9BAH6rQp8aU";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/cpd/api/ProductRead/GetAllProductInfo?pageindex=" + pindex + "&pagesize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }


        //产品
        public class Products
        {
            /// <summary>
            /// 产品id
            /// </summary>
            public int ProductsID { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductsCode { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductsName { get; set; }
            /// <summary>
            /// 厂商编码
            /// </summary>
            public string? VendorsCode { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InteriorCode { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? Specs { get; set; }
            /// <summary>
            /// 产品类别
            /// </summary>
            public int? ProductTypeID { get; set; }
            /// <summary>
            /// 存储单位
            /// </summary>
            public string? StorageUnits { get; set; }
            /// <summary>
            /// 预警（上）
            /// </summary>
            public int? Upperlimit { get; set; }
            /// <summary>
            /// 预警（下）
            /// </summary>
            public int? Lowerlimit { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public int? WrapType { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? Price { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public decimal? Weight { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 库位
            /// </summary>
            public int? StorehouseId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public int? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public int? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? Describe { get; set; }
            /// <summary>
            /// 产品数量
            /// </summary>
            public int? ProcurementNum { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }
    }
}
